//
//  ScreenTypeView.m
//  MiBaoTu
//
//  Created by admin on 16/9/26.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "ScreenTypeView.h"

@interface ScreenTypeView () {
    UIView              *bgView;
    NSArray             *_dataArr;
    int                  selectH1Index;
    int                  selectH2Index;
    int                  selectH3Index;
    BOOL                 isOne;
    BOOL                 isTwo;
    BOOL                 isThree;
}

@end
@implementation ScreenTypeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self onCreateUI];
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
    }
    return self;
}

- (void)onCreateUI
{
    isOne = NO;
    isTwo = NO;
    isThree = NO;
    
    self.backgroundColor = [UIColorFromHex(0x000000)colorWithAlphaComponent:.2];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTapForHideSelf)];
    [self addGestureRecognizer:tap];
    
    _dataArr = [[NSArray alloc]initWithObjects:@[@"儿歌",@"故事",@"英文",@"国学"],@[@"0-2岁",@"3-4岁",@"5-6岁",@"6岁以上"],@[@"睡前安抚",@"父母学堂",@"习性养成",@"智力开发"], nil];
    selectH1Index = selectH2Index = selectH3Index = 100;
    
    bgView = [[UIView alloc]initWithFrame:CGRectMake(0, (ISPortrait?-320*PERCEN:-640*PERCEN), self.width, 320*PERCEN )];
    if (ISIPAD) {
        bgView.frame = CGRectMake(SCREEN_WIDTH-800*PERCEN, -SCREEN_HEIGHT, 800*PERCEN, SCREEN_HEIGHT);
    }
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    
    //分类
    int tagIndex = 5000;
    for (int i=0; i<3; i++) {
        UILabel *_nickNameLb = [[UILabel alloc]initWithFrame:CGRectMake(25*PERCEN, 40*PERCEN+90*PERCEN*i, 80*PERCEN, 50*PERCEN)];
        if (ISIPAD) {
            _nickNameLb.top = 90*PERCEN+180*PERCEN*i;
        }
        _nickNameLb.backgroundColor = [UIColor clearColor];
        _nickNameLb.textAlignment = NSTextAlignmentLeft;
        _nickNameLb.textColor = TINK_BG_COLOR;
        _nickNameLb.font = [UIFont systemFontOfSize:13.f];
        [bgView addSubview:_nickNameLb];
        
        NSArray *arr = [_dataArr objectAtIndex:i];
        for (int j=0; j<4; j++) {
            UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
            bt.backgroundColor = [UIColor whiteColor];
            bt.frame = CGRectMake(_nickNameLb.right+160*PERCEN*j, 40*PERCEN+(40*PERCEN+(ISIPAD?140*PERCEN:50*PERCEN))*i, 140*PERCEN, (ISIPAD?140*PERCEN:50*PERCEN));
            [bt setTitle:[arr objectAtIndex:j] forState:UIControlStateNormal];
            [bt setTitleColor:TINK_COLOR forState:UIControlStateNormal];
            [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            bt.titleLabel.font = [UIFont systemFontOfSize:13.f];
            bt.layer.cornerRadius = (ISIPAD?70*PERCEN:5.f);
            bt.layer.masksToBounds = YES;
            bt.layer.borderWidth = 1.f;
            bt.layer.borderColor = TINK_COLOR.CGColor;
            bt.tag = tagIndex;
            [bt addTarget:self action:@selector(onSelectItemAction:) forControlEvents:UIControlEventTouchUpInside];
            [bgView addSubview:bt];
            
            ++tagIndex;
        }
        
        if (i == 0) {
            _nickNameLb.text = @"分类:";
        }
        else if (i == 1) {
            _nickNameLb.text = @"分龄:";
        }
        else if (i == 2) {
            _nickNameLb.text = @"场景:";
        }
    }
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
}

- (void)orientChange:(NSNotification *)noti {
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    [self onTurnToOriention:orientation];
}

- (void)onTurnToOriention:(UIDeviceOrientation)orientation
{
    if (ISIPAD) {
        self.frame = CGRectMake(0, self.top, SCREEN_WIDTH, (self.height>0?SCREEN_HEIGHT:self.height));
        bgView.frame = CGRectMake(SCREEN_WIDTH-800*PERCEN,bgView.top, 800*PERCEN, SCREEN_HEIGHT);
    } else {
        self.frame = CGRectMake(0, NAV_HEIGHT, SCREEN_WIDTH, self.height>0?SCREEN_HEIGHT:self.height);
        bgView.frame = CGRectMake(0, bgView.top, self.width, bgView.height);
    }
}


- (void)onSelectItemAction:(UIButton *)bt
{
    NSLog(@"bt tag :%d",(int)bt.tag);
    int index = (int)bt.tag - 5000;
    int h = index / 4; //行数
    bt.backgroundColor = TINK_COLOR;

    if (h == 0) {
        if (selectH1Index == 100) {
            //第一次
            bt.selected = !bt.selected;
            isOne = YES;
        } else {
            bt.selected = !bt.selected;
            if (selectH1Index != index && bt.selected == YES) {
                UIButton *lastBt = (UIButton *)[self viewWithTag:(5000+selectH1Index)];
                lastBt.selected = !bt.selected;
                lastBt.backgroundColor = [UIColor whiteColor];
                isOne = YES;
            } else {
                //同一个按钮点击
                if (bt.selected == NO) {
                    bt.backgroundColor = [UIColor whiteColor];
                }
                isOne = bt.selected;
            }
        }
        selectH1Index = index;
    } else if (h == 1){
        if (selectH2Index == 100) {
            //第一次
            bt.selected = !bt.selected;
            isTwo = YES;
        } else {
            bt.selected = !bt.selected;
            if (selectH2Index != index && bt.selected == YES) {
                UIButton *lastBt = (UIButton *)[self viewWithTag:(5000+selectH2Index)];
                lastBt.selected = !bt.selected;
                lastBt.backgroundColor = [UIColor whiteColor];
                isTwo = YES;
            } else {
                //同一个按钮点击
                if (bt.selected == NO) {
                    bt.backgroundColor = [UIColor whiteColor];
                }
                isTwo = bt.selected;
            }
        }
        selectH2Index = index;
    }
    else if (h == 2){
        if (selectH3Index == 100) {
            //第一次
            bt.selected = !bt.selected;
            isThree = YES;
        } else {
            bt.selected = !bt.selected;
            if (selectH3Index != index && bt.selected == YES) {
                UIButton *lastBt = (UIButton *)[self viewWithTag:(5000+selectH3Index)];
                lastBt.selected = !bt.selected;
                lastBt.backgroundColor = [UIColor whiteColor];
                isThree = YES;
            } else {
                //同一个按钮点击
                if (bt.selected == NO) {
                    bt.backgroundColor = [UIColor whiteColor];
                }
                isThree = bt.selected;
            }
        }
        selectH3Index = index;
    }
    
    
    if (_delegate && [_delegate respondsToSelector:@selector(onShowScreenTypeWithFenLei:withFenLing:withChangJiang:withView:)]) {
        
        [_delegate onShowScreenTypeWithFenLei:(isOne?selectH1Index:100) withFenLing:(isTwo?selectH2Index:100) withChangJiang:(isThree?selectH3Index:100) withView:self];
    }
}

- (void)showInView:(BOOL )isShow
{
//    [UIView animateWithDuration:.35 animations:^{
        if (ISIPAD) {
            bgView.top = isShow ? 0 : -SCREEN_HEIGHT;
            self.height = isShow ? SCREEN_HEIGHT: 0;
        } else {
            bgView.top = isShow ? 0 : -320*PERCEN*(ISPortrait?1:2);
            self.height = isShow ? SCREEN_HEIGHT: 0;
        }
//     [self onTurnToOriention:_orientation];
//    }];
}

- (void)onTapForHideSelf
{
    [self showInView:NO];
}

@end
